package com.mallcloud.mall.admin.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mallcloud.mall.admin.api.entity.SysRegion;
import com.mallcloud.mall.admin.service.SysRegionService;
import com.mallcloud.mall.common.core.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "全国省市区管理")
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/region")
public class SysRegionController {
    private final SysRegionService sysRegionService;

    @ApiOperation("分页查询")
    @GetMapping("/page")
    public R getSysRegionPage(Page page, SysRegion sysRegion) {
        return R.ok(sysRegionService.page(page, Wrappers.query(sysRegion)));
    }

    @ApiOperation("获取省级行政区")
	@GetMapping("/getProvince")
	public R getProvince() {
		SysRegion sysRegion = new SysRegion();
		sysRegion.setParentId(10000000L);
		sysRegion.setLevelId("1");
		return R.ok(sysRegionService.list(Wrappers.query(sysRegion)));
	}

	@ApiOperation("获取市级行政区")
	@GetMapping("/getCity/{code}")
	public R getCity(@PathVariable("code") Long code) {
		return R.ok(sysRegionService.getCity(code));
	}

	@ApiOperation("获取区级行政区")
	@GetMapping("/getArea/{code}")
	public R getArea(@PathVariable("code") Long code) {
		return R.ok(sysRegionService.getArea(code));
	}

	@ApiOperation("根据pid查询下级行政区")
	@GetMapping("/pid/{pid}")
	public R getSysRegionById(@PathVariable("pid") Long pid) {
		SysRegion sysRegion = new SysRegion();
		sysRegion.setParentId(pid);
		return R.ok(sysRegionService.list(Wrappers.query(sysRegion)));
	}

	@ApiOperation("通过id查询")
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(sysRegionService.getById(id));
    }
}
